package com.rxb.blog.controller;

import com.rxb.blog.common.CommonResult;
import com.rxb.blog.entity.ArticleTypeEntity;
import com.rxb.blog.service.ArticleTypeService;
import com.rxb.blog.utils.BlogException;
import com.rxb.blog.vo.ArticleTypeVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Author：rxb
 * Date：2020-02-28 15:47
 * Description：博客类型相关接口
 */
@Api(description = "博客类型相关接口")
@RestController
@RequestMapping("articleType")
public class ArticleTypeController {
    @Autowired
    private ArticleTypeService articleTypeService;


    @ApiOperation(value = "获取当前用户下所有博客类型（分页）", notes="获取当前用户下所有博客类型（分页）")
    @PostMapping("getArticleTypes")
    public CommonResult<Map> getArticleTypes(@RequestBody Map<String,Object> map,HttpSession session) {
        map.put("username",(String)session.getAttribute("username"));
        Map articleTypes = articleTypeService.getArticleTypes(map);
        return CommonResult.success(articleTypes);
    }

    @ApiOperation(value = "首页获取所有文章类型(文章类型-后台管理系统操作)", notes="首页获取所有文章类型(文章类型-后台管理系统操作)")
    @PostMapping("getArticleTypesForHome")
    public CommonResult<List<Map>> getArticleTypesForHome() {
        List<Map> articleTypes = articleTypeService.getArticleTypesForHome();
        return CommonResult.success(articleTypes);
    }

    @ApiOperation(value = "新增/修改博客类型", notes="新增、修改博客类型")
    @ApiImplicitParam(name = "articleTypeVo", value = "博客类型实体vo", paramType = "query", required = true, dataType = "ArticleTypeVo")
    @PostMapping("insertArticleType")
    public CommonResult<String>  insertArticleType(@RequestBody ArticleTypeVo articleTypeVo, HttpSession session){
        String username = (String) session.getAttribute("username");
        articleTypeVo.setUsername(username);
        try {
            if(null != articleTypeVo.getId()){
                articleTypeService.updateArticleType(articleTypeVo);
                return CommonResult.success("修改博客类型成功");
            }else{
                articleTypeService.insertArticleType(articleTypeVo);
                return CommonResult.success("新增博客类型成功");
            }
        }catch (BlogException e){
            return CommonResult.failed(e.getMessage());
        }
    }

    @ApiOperation(value = "根据id获取当前博客类型", notes="根据id获取当前博客类型")
    @ApiImplicitParam(name = "id", value = "博客类型id", paramType = "query", required = true, dataType = "int")
    @GetMapping("getArticleTypeById")
    public CommonResult<ArticleTypeEntity> getArticleTypeById(@RequestParam("id") int id){
        ArticleTypeEntity article = articleTypeService.getArticleTypeById(id);
        return CommonResult.success(article);
    }


    @ApiOperation(value = "删除博客类型", notes="删除博客类型")
    @PostMapping("deleteArticleType")
    public void deleteArticleType(@RequestBody Map<String,Object> map){
        articleTypeService.deleteArticleType(map);
    }

    @ApiOperation(value = "查询所有的博客类型（不分页）", notes="查询所有的博客类型（不分页）")
    @GetMapping("getArticleTypes") //不分页
    public CommonResult<List<ArticleTypeEntity>> getArticleTypes(HttpSession session){
        Map map = new HashMap();
        map.put("username",session.getAttribute("username").toString());
        List<ArticleTypeEntity> types = articleTypeService.getArticleTypesAll(map);
        return CommonResult.success(types);
    }
}
